<?php

namespace App\Service\Activity;

use App\Enums\OpenShop\MissionTemplateEnum;
use App\Error\KfAdmin\ShopOrderActivityError;
use App\Exceptions\KfHttpResponseException;
use App\Http\Resources\Activity\B\ShopOrderActivityDataStatisticsResource;
use App\Http\Resources\Activity\B\ShopOrderActivityDetailResource;
use App\Http\Resources\Activity\B\ShopOrderActivityListResource;
use App\Jobs\Activity\HandleExpiredStatusJob;
use App\Library\Rms\RmsShopApi;
use App\Models\Activity\ShopOrderActivityFeedbackModel;
use App\Models\Activity\ShopOrderActivityModel;
use App\Models\Activity\ShopOrderActivityShopJoinRelModel;
use App\Models\Activity\ShopOrderActivityShopRelModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\Shop\ShopModel;
use App\Service\BaseService;
use App\Service\OpenShop\Feishu\ConfigService;
use App\Service\OpenShop\Feishu\KfosService;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\HigherOrderBuilderProxy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\DB;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ShopOrderActivityService extends BaseService
{
    /**
     * 列表
     *
     * @param object $params
     * @return array
     */
    public static function  list(object $params): array
    {
        $paginate = ShopOrderActivityModel::query()
                                          ->with(["createdUserRel"])
                                          ->withCount("shopRel as shopRelCount")
                                          ->when(checkFieldLen($params, "name"), function ($query) use ($params) {
                                              $query->where("name", "like", "%" . $params->name . "%");
                                          })
                                          ->when(checkFieldLen($params, "type"), function ($query) use ($params) {
                                              $query->where("type", $params->type);
                                          })
                                          ->when(checkFieldLen($params, "startTime"), function ($query) use ($params) {
                                              $query->where(function ($query) use ($params) {
                                                  $query->where("expireType", ShopOrderActivityModel::EXPIRE_TYPE_0)
                                                        ->oRwhere(
                                                            function ($query) use ($params) {
                                                                $query->where("startTime", ">=",
                                                                    strtotime($params->startTime))
                                                                      ->where("startTime", "<=", strtotime("+1 day",
                                                                              strtotime($params->endTime)) - 1);
                                                            })
                                                        ->oRwhere(
                                                            function ($query) use ($params) {
                                                                $query->where("endTime", ">=",
                                                                    strtotime($params->startTime))
                                                                      ->where("endTime", "<=", strtotime("+1 day",
                                                                              strtotime($params->endTime)) - 1
                                                                      );
                                                            })
                                                        ->oRwhere(
                                                            function ($query) use ($params) {
                                                                $query->where("endTime", ">=", strtotime("+1 day",
                                                                        strtotime($params->endTime)) - 1)
                                                                      ->where("startTime", "<=",
                                                                          strtotime($params->startTime));
                                                            }
                                                        );
                                              });
                                          })
                                          ->when(checkFieldLen($params, "createdByUid"),
                                              function ($query) use ($params) {
                                                  $query->where("createdByUid", $params->createdByUid);
                                              })
                                          ->when(checkFieldLen($params, "shopId"), function ($query) use ($params) {
                                              $query->where(function ($query) use ($params) {
                                                  $query->where("shopType", ShopOrderActivityModel::SHOP_TYPE_ALL)
                                                        ->orWhere(function ($query) use ($params) {
                                                            $query->where("shopType",
                                                                ShopOrderActivityModel::SHOP_TYPE_PART)
                                                                  ->whereHas("shopRel",
                                                                      function ($query) use ($params) {
                                                                          $query->where("shopId", $params->shopId);
                                                                      });
                                                        });
                                              });
                                          })
                                          ->when(checkFieldLen($params, "status"), function ($query) use ($params) {
                                              $query->where("status", $params->status);
                                          })
                                          ->orderByDesc("id")
                                          ->paginate($params->pageSize, ['*'], 'pageNum', $params->pageNum);

        return [
            "list"     => ShopOrderActivityListResource::collection($paginate->items()),
            "count"    => $paginate->total(),
            "pageNum"  => $params->pageNum,
            "pageSize" => $params->pageSize,
        ];
    }

    /**
     * 新增
     *
     * @param int $uid
     * @param object $params
     * @throws Exception
     */
    public static function insert(int $uid, object $params)
    {
        try {
            DB::beginTransaction();
            //todo 主表写入
            $mid = ShopOrderActivityModel::query()->insertGetId(
                collect((array)$params)->merge([
                    "createdByUid" => $uid,
                    "updatedByUid" => $uid,
                    "createdAt"    => time(),
                    "updatedAt"    => time()
                ])->when(
                    $params->expireType == ShopOrderActivityModel::EXPIRE_TYPE_0,
                    function ($query) {
                        return $query->except(["startTime", "endTime"]);
                    },
                    function ($query) use ($params) {
                        return $query->merge([
                            "startTime" => strtotime($params->startTime),
                            "endTime"   => strtotime("+1 day", strtotime($params->endTime)) - 1,
                        ]);
                    }
                )->except(["shopIds"])->toArray()
            );

            // todo 限制参与门店配置表写入
            if ($params->shopType == ShopOrderActivityModel::SHOP_TYPE_PART) {
                ShopOrderActivityShopRelModel::query()->insert(collect($params->shopIds)->map(
                    function (int $shopId) use ($mid, $uid) {
                        return [
                            "shopOrderActivityId" => $mid,
                            "shopId"              => $shopId,
                            "createdByUid"        => $uid,
                            "updatedByUid"        => $uid,
                            "createdAt"           => time(),
                            "updatedAt"           => time()
                        ];
                    })->values()->toArray()
                );
            }
            DB::commit();

            // todo 限时活动延时队列进行状态维护
            if ($params->expireType == ShopOrderActivityModel::EXPIRE_TYPE_1) {
                dispatch(new HandleExpiredStatusJob($mid))
                    ->delay(strtotime("+1 day", strtotime($params->endTime)) - 1 - time());
            }


        } catch (Exception $exception) {
            DB::rollBack();
            handleKfException($exception);
        }
    }

    /**
     * 编辑
     *
     * @param int $uid
     * @param object $params
     * @throws Exception
     */
    public static function update(int $uid, object $params)
    {
        try {
            DB::beginTransaction();
            //todo 主表变更
            ShopOrderActivityModel::query()->where("id", $params->id)->update(
                collect((array)$params)->merge([
                    "updatedByUid" => $uid,
                    "updatedAt"    => time()
                ])->when(
                    $params->expireType == ShopOrderActivityModel::EXPIRE_TYPE_0,
                    function ($query) {
                        return $query->merge([
                            "startTime" => 0,
                            "endTime"   => 0,
                        ]);
                    },
                    function ($query) use ($params) {
                        return $query->merge([
                            "startTime" => strtotime($params->startTime),
                            "endTime"   => strtotime("+1 day", strtotime($params->endTime)) - 1,
                        ]);
                    }
                )->except(["shopIds", "id"])->toArray()
            );


            // todo 限制参与门店配置表写入
            switch ($params->shopType) {
                case ShopOrderActivityModel::SHOP_TYPE_PART:
                    $existsShopIds       = ShopOrderActivityShopRelModel::query()
                                                                        ->withoutGlobalScope("isDel")
                                                                        ->where("shopOrderActivityId", $params->id)
                                                                        ->pluck("shopId")->toArray();
                    $requestShopIds      = $params->shopIds;
                    $intersectionShopIds = array_intersect($existsShopIds, $requestShopIds);
                    $delShopIds          = array_diff($existsShopIds, $intersectionShopIds);
                    if ($delShopIds) {
                        // todo 未选中数据删除
                        ShopOrderActivityShopRelModel::query()
                                                     ->withoutGlobalScope("isDel")
                                                     ->where("shopOrderActivityId", $params->id)
                                                     ->whereIn("shopId", $delShopIds)
                                                     ->update([
                                                         "isDeleted" => 1,
                                                         "updatedAt" => time()
                                                     ]);
                    }

                    if ($intersectionShopIds) {
                        // todo 已存在数据状态变更
                        ShopOrderActivityShopRelModel::query()
                                                     ->withoutGlobalScope("isDel")
                                                     ->where("shopOrderActivityId", $params->id)
                                                     ->whereIn("shopId", $intersectionShopIds)
                                                     ->update([
                                                         "isDeleted" => 0,
                                                         "updatedAt" => time()
                                                     ]);
                    }

                    $diffShopIds = array_diff($requestShopIds, $intersectionShopIds);
                    if ($diffShopIds) {
                        // todo 新增数据写入
                        ShopOrderActivityShopRelModel::query()->insert(collect($diffShopIds)->map(
                            function (int $shopId) use ($params, $uid) {
                                return [
                                    "shopOrderActivityId" => $params->id,
                                    "shopId"              => $shopId,
                                    "createdByUid"        => $uid,
                                    "updatedByUid"        => $uid,
                                    "createdAt"           => time(),
                                    "updatedAt"           => time()
                                ];
                            })->values()->toArray()
                        );
                    }

                    break;
                case ShopOrderActivityModel::SHOP_TYPE_ALL:
                    // todo 所有门店 此时删除已存在数据
                    ShopOrderActivityShopRelModel::query()->where("shopOrderActivityId", $params->id)->update([
                        "isDeleted" => 1,
                        "updatedAt" => time()
                    ]);
                    break;
            }

            DB::commit();

            // todo 限时活动延时队列进行状态维护
            if ($params->expireType == ShopOrderActivityModel::EXPIRE_TYPE_1) {
                dispatch(new HandleExpiredStatusJob($params->id))
                    ->delay(strtotime("+1 day", strtotime($params->endTime)) - 1 - time());
            }
        } catch (Exception $exception) {
            DB::rollBack();
            handleKfException($exception);
        }


    }

    /**
     * 发布|暂停
     *
     * @param int $uid
     * @param $params
     */
    public static function status(int $uid, $params)
    {
        $row              = ShopOrderActivityModel::query()->where("id", $params->id)->first();
        $canOperateConfig = [
            ShopOrderActivityModel::STATUS_0 => ShopOrderActivityModel::STATUS_1,
            ShopOrderActivityModel::STATUS_1 => ShopOrderActivityModel::STATUS_2,
            ShopOrderActivityModel::STATUS_2 => ShopOrderActivityModel::STATUS_1,
        ];

        if (
            !in_array($row->status, array_keys($canOperateConfig))
            ||
            $canOperateConfig[$row->status] != $params->status
        ) {
            throw new KfHttpResponseException(ShopOrderActivityError::OPERATE_STATUS_ERROR);
        }

        $row->update([
            "status"       => $params->status,
            "updatedByUid" => $uid,
            "updatedAt"    => time()
        ]);
    }

    /**
     * 详情
     *
     * @param int $id
     * @return array
     */
    public static function detail(int $id): array
    {
        $detail = ShopOrderActivityModel::query()->with([
            "shopRel" => function ($query) {
                $query->with([
                    "shopDetailRel" => function ($query) {
                        $query->select(["id", "name"]);
                    }
                ])->select(["id", "shopOrderActivityId", "shopId"]);
            }
        ])->where("id", $id)->first();

        return [
            "detail" => ShopOrderActivityDetailResource::collection([$detail])->getIterator()->current()
        ];
    }

    /**
     * 结束
     *
     * @param int $uid
     * @param int $id
     */
    public static function finish(int $uid, int $id)
    {
        ShopOrderActivityModel::query()->where("id", $id)->update([
            "status"       => ShopOrderActivityModel::STATUS_3,
            "updatedByUid" => $uid,
            "updatedAt"    => time()
        ]);
    }

    /**
     * 获取申请记录
     *
     * @param int $activity
     * @param int $shopId
     * @return HigherOrderBuilderProxy|int|mixed
     */
    public static function getAppliedStatus(int $activity, int $shopId)
    {
        $result = ShopOrderActivityShopJoinRelModel::query()
                                                   ->where('shopOrderActivityId', $activity)
                                                   ->where('shopId', $shopId)
                                                   ->first();

        return $result == null ? ShopOrderActivityShopJoinRelModel::STATUS_NOT_APPLY : $result->status;
    }

    /**
     * 门店是否能参加活动
     *
     * @param int $activity
     * @param int $shopId
     * @return HigherOrderBuilderProxy|int|mixed
     */
    public static function getShopIsCanApply(int $activity, int $shopId)
    {
        // 查询活动信息
        $activityInfo = ShopOrderActivityModel::query()->where('id', $activity)->first();

        if ($activityInfo->shopType == ShopOrderActivityModel::SHOP_TYPE_ALL) {
            return true;
        }

        $result = ShopOrderActivityShopRelModel::query()
                                               ->where('shopOrderActivityId', $activity)
                                               ->where('shopId', $shopId)
                                               ->first();

        return !empty($result);
    }

    /**
     * @param object $params
     * @return array
     */
    public static function dataStatistics(object $params): array
    {
        $paginate = ShopOrderActivityModel::query()
                                          ->withCount("shopRel as shopRelCount")
                                          ->withCount("shopJoinRel as shopJoinRelCount")
                                          ->withCount("shopFeedbackRel as shopFeedbackRelCount")
                                          ->when(checkFieldLen($params, "id"), function ($query) use ($params) {
                                              $query->where("id", $params->id);
                                          })
                                          ->when(checkFieldLen($params, "name"), function ($query) use ($params) {
                                              $query->where("name", "like", "%" . $params->name . "%");
                                          })
                                          ->when(checkFieldLen($params, "type"), function ($query) use ($params) {
                                              $query->where("type", $params->type);
                                          })
                                          ->when(checkFieldLen($params, "startTime"), function ($query) use ($params) {
                                              $query->where(function ($query) use ($params) {
                                                  $query->where("expireType", ShopOrderActivityModel::EXPIRE_TYPE_0)
                                                        ->oRwhere(
                                                            function ($query) use ($params) {
                                                                $query->where("startTime", ">=",
                                                                    strtotime($params->startTime))
                                                                      ->where("startTime", "<=", strtotime("+1 day",
                                                                              strtotime($params->endTime)) - 1);
                                                            })
                                                        ->oRwhere(
                                                            function ($query) use ($params) {
                                                                $query->where("endTime", ">=",
                                                                    strtotime($params->startTime))
                                                                      ->where("endTime", "<=", strtotime("+1 day",
                                                                              strtotime($params->endTime)) - 1
                                                                      );
                                                            })
                                                        ->oRwhere(
                                                            function ($query) use ($params) {
                                                                $query->where("endTime", ">=", strtotime("+1 day",
                                                                        strtotime($params->endTime)) - 1)
                                                                      ->where("startTime", "<=",
                                                                          strtotime($params->startTime));
                                                            }
                                                        );
                                              });
                                          })
                                          ->when(checkFieldLen($params, "createdByUid"),
                                              function ($query) use ($params) {
                                                  $query->where("createdByUid", $params->createdByUid);
                                              })
                                          ->when(checkFieldLen($params, "shopId"), function ($query) use ($params) {
                                              $query->where(function ($query) {
                                                  $query->where("shopType", ShopOrderActivityModel::SHOP_TYPE_ALL)
                                                        ->orWhere(function ($query) {
                                                            $query->where("shopType",
                                                                ShopOrderActivityModel::SHOP_TYPE_PART)
                                                                  ->whereHas("shopRel");
                                                        });
                                              });
                                          })
                                          ->when(checkFieldLen($params, "status"), function ($query) use ($params) {
                                              $query->where("status", $params->status);
                                          })
                                          ->orderByDesc("id")
                                          ->paginate($params->pageSize, ['*'], 'pageNum', $params->pageNum);


        return [
            "list"     => ShopOrderActivityDataStatisticsResource::collection($paginate->items()),
            "count"    => $paginate->total(),
            "pageNum"  => $params->pageNum,
            "pageSize" => $params->pageSize,
        ];
    }

    /**
     * 数据统计门店列表
     *
     * @param object $params
     * @return Builder[]|Collection|array
     */
    public static function dataStatisticsShopList(object $params)
    {
        $activityModel = ShopOrderActivityModel::query()->where("id", $params->id)->first();
        switch ($params->businessType) {
            case ShopOrderActivityModel::BUSINESS_TYPE_0:
                if ($activityModel->shopType == ShopOrderActivityModel::SHOP_TYPE_ALL) {
                    // todo 全部门店 未报名活动
                    $paginate = ShopModel::query()->orderByDesc("id")
                                         ->select(["id as shopId", "shopCode", "name"])
                                         ->when(checkFieldLen($params, "shopId"), function ($query) use ($params) {
                                             $query->where("id", $params->shopId);
                                         })
                                         ->when(
                                             $params->export,
                                             function ($query) {
                                                 return $query->get();
                                             },
                                             function ($query) use ($params) {
                                                 return $query->paginate($params->pageSize, ['*'], 'pageNum',
                                                     $params->pageNum);
                                             }
                                         );
                } else {
                    // todo 部分门店 适用门店
                    $paginate = ShopModel::query()->whereIn(
                        "id",
                        function ($query) use ($params) {
                            $query->select("shopId")->from((new ShopOrderActivityShopRelModel())->getTable())
                                  ->where("shopOrderActivityId", $params->id);
                        })->when(
                        checkFieldLen($params, "shopId"),
                        function ($query) use ($params) {
                            $query->where("id", $params->shopId);
                        })->orderByDesc("id")->select(["id as shopId", "shopCode", "name"])->when(
                        $params->export,
                        function ($query) {
                            return $query->get();
                        },
                        function ($query) use ($params) {
                            return $query->paginate($params->pageSize, ['*'],
                                'pageNum', $params->pageNum);
                        }
                    );
                }

                $list = (bool)$params->export ? $paginate : collect($paginate->items())->toArray();
                break;
            case ShopOrderActivityModel::BUSINESS_TYPE_1:
                $paginate = ShopOrderActivityShopJoinRelModel::query()
                                                             ->with("shopDetailRel")
                                                             ->when(checkFieldLen($params, "shopId"),
                                                                 function ($query) use ($params) {
                                                                     $query->where("shopId", $params->shopId);
                                                                 })
                                                             ->where("shopOrderActivityId", $params->id)
                                                             ->where("status",
                                                                 ShopOrderActivityShopJoinRelModel::STATUS_APPLY_IN)
                                                             ->orderByDesc("id")
                                                             ->when(
                                                                 $params->export,
                                                                 function ($query) {
                                                                     return $query->get();
                                                                 },
                                                                 function ($query) use ($params) {
                                                                     return $query->paginate($params->pageSize, ['*'],
                                                                         'pageNum', $params->pageNum);
                                                                 }
                                                             );

                $list = (((bool)$params->export) ? $paginate : collect($paginate->items()))->map(
                    function (ShopOrderActivityShopJoinRelModel $activityShopJoinRelModel) {
                        return [
                            "shopId"        => $activityShopJoinRelModel->shopId,
                            "shopCode"      => attributeGet(
                                $activityShopJoinRelModel, "shopDetailRel.shopCode", ""
                            ),
                            "name"          => attributeGet($activityShopJoinRelModel, "shopDetailRel.name", ""),
                            "createdAtText" => $activityShopJoinRelModel->createdAt->format("Y-m-d H:i:s"),
                        ];
                    })->values()->toArray();
                break;
            case ShopOrderActivityModel::BUSINESS_TYPE_2:
                if ($activityModel->shopType == ShopOrderActivityModel::SHOP_TYPE_ALL) {
                    // todo 全部门店 未报名活动
                    $paginate = ShopModel::query()->whereNotIn(
                        "id",
                        function ($query) use ($params) {
                            $query->select("shopId")->from((new ShopOrderActivityShopJoinRelModel())->getTable())
                                  ->where("status", ShopOrderActivityShopJoinRelModel::STATUS_APPLY_IN)
                                  ->where("shopOrderActivityId", $params->id);
                        }
                    )->when(checkFieldLen($params, "shopId"), function ($query) use ($params) {
                        $query->where("id", $params->shopId);
                    })->orderByDesc(
                        "id"
                    )->select(
                        ["id as shopId", "shopCode", "name"]
                    )->when(
                        $params->export,
                        function ($query) {
                            return $query->get();
                        },
                        function ($query) use ($params) {
                            return $query->paginate($params->pageSize, ['*'],
                                'pageNum', $params->pageNum);
                        }
                    );
                } else {
                    // todo 部分门店 未报名活动
                    $paginate = ShopModel::query()->when(checkFieldLen($params, "shopId"),
                        function ($query) use ($params) {
                            $query->where("id", $params->shopId);
                        })->whereIn(
                        "id",
                        function ($query) use ($params) {
                            $query->select("shopId")->from((new ShopOrderActivityShopRelModel())->getTable())
                                  ->where("shopOrderActivityId", $params->id)
                                  ->whereNotIn("ShopId", function ($query) use ($params) {
                                      $query->select("shopId")
                                            ->from((new ShopOrderActivityShopJoinRelModel())->getTable())
                                            ->where("status", ShopOrderActivityShopJoinRelModel::STATUS_APPLY_IN)
                                            ->where("shopOrderActivityId", $params->id);
                                  });
                        })->orderByDesc("id")->select(["id as shopId", "shopCode", "name"])->when(
                        $params->export,
                        function ($query) {
                            return $query->get();
                        },
                        function ($query) use ($params) {
                            return $query->paginate($params->pageSize, ['*'],
                                'pageNum', $params->pageNum);
                        }
                    );
                }

                $list = (bool)$params->export ? $paginate : collect($paginate->items())->toArray();
                break;
            case ShopOrderActivityModel::BUSINESS_TYPE_3:
                $paginate = ShopOrderActivityFeedbackModel::query()
                                                          ->with("shopDetailRel")
                                                          ->with("createdUserRel")
                                                          ->when(checkFieldLen($params, "shopId"),
                                                              function ($query) use ($params) {
                                                                  $query->where("shopId", $params->shopId);
                                                              })
                                                          ->where("activityId", $params->id)
                                                          ->orderByDesc("id")
                                                          ->when(
                                                              $params->export,
                                                              function ($query) {
                                                                  return $query->get();
                                                              },
                                                              function ($query) use ($params) {
                                                                  return $query->paginate($params->pageSize, ['*'],
                                                                      'pageNum', $params->pageNum);
                                                              }
                                                          );

                $list = ((bool)$params->export ? $paginate : collect($paginate->items()))->map(
                    function (ShopOrderActivityFeedbackModel $activityFeedbackModel) {
                        return [
                            "shopId"          => $activityFeedbackModel->shopId,
                            "shopCode"        => attributeGet(
                                $activityFeedbackModel, "shopDetailRel.shopCode", ""
                            ),
                            "name"            => attributeGet($activityFeedbackModel, "shopDetailRel.name", ""),
                            "createdUsername" => $activityFeedbackModel->createdUserRel ?
                                vsprintf("%s(%s)", [
                                    $activityFeedbackModel->createdUserRel->name,
                                    $activityFeedbackModel->createdUserRel->nickname
                                ])
                                : "",
                            "shopContact"     => $activityFeedbackModel->shopContact,
                            "feedback"        => $activityFeedbackModel->feedback,
                            "feedbackImages"  => $activityFeedbackModel->feedbackImages,
                            "feedbackVideos"  => $activityFeedbackModel->feedbackVideos,
                            "createdAtText"   => $activityFeedbackModel->createdAt->format("Y-m-d H:i:s"),
                        ];
                    })->toArray();
                break;
            case ShopOrderActivityModel::BUSINESS_TYPE_4:
                // todo 部分门店 未报名活动
                $paginate = ShopModel::query()->whereIn(
                    "id",
                    function ($query) use ($params) {
                        $query->select("shopId")->from((new ShopOrderActivityShopJoinRelModel())->getTable())
                              ->where("shopOrderActivityId", $params->id)
                              ->where("status", ShopOrderActivityShopJoinRelModel::STATUS_APPLY_IN)
                              ->whereNotIn("ShopId", function ($query) use ($params) {
                                  $query->select("shopId")
                                        ->from((new ShopOrderActivityFeedbackModel())->getTable())
                                        ->where("activityId", $params->id);
                              });
                    })->orderByDesc("id")->select(["id as shopId", "shopCode", "name"])->when(
                    $params->export,
                    function ($query) {
                        return $query->get();
                    },
                    function ($query) use ($params) {
                        return $query->paginate($params->pageSize, ['*'],
                            'pageNum', $params->pageNum);
                    }
                );

                $list = (bool)$params->export ? $paginate : collect($paginate->items())->toArray();
                break;
        }


        return (bool)$params->export ? $list : [
            "list"     => $list,
            "count"    => $paginate->total(),
            "pageNum"  => $params->pageNum,
            "pageSize" => $params->pageSize,
        ];
    }

    /**
     * 获取门店活动详情
     *
     * @param int $activityId
     * @param int $shopId
     * @return array
     * @throws GuzzleException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function getActivityInfo(int $activityId, int $shopId): array
    {
        $detail    = ShopOrderActivityModel::query()->where('id', $activityId)->first() ?? [];
        $shopModel = ShopModel::query()->where("id", $shopId)->first();

        // 判断门店督导是否是当前用户
        $shopCouncilorId = $shopModel->councilorId;
        $operateUid      = (int)optional(getRmsUserInfoByAuth())->id;

        if (empty($detail)) {
            return BaseService::returnData(0, "活动不存在");
        }

        $openMain = OpenShopMainProcessModel::query()->where('shopId', $shopId)->orderBy('id')->first();
//        $openMain = OpenShopMainProcessModel::query()->where('shopId', $shopId)->first();

        return BaseService::returnData(1, '', [
            // 详情
            'detail'         => ShopOrderActivityDetailResource::collection([$detail])->getIterator()->current(),
            // 是否已经回传照片信息
            'isFeedback'     => ShopOrderActivityFeedbackService::getAppliedStatus($activityId, $shopId),
            // 是否是活动门店
            'isShopCanApply' => ShopOrderActivityService::getShopIsCanApply($activityId, $shopId),
            // 是否存在订货单
            'isExistOrder'   => KfosService::checkHasFirstDeliveryNotes($shopId),
            // 是否存在订货建党
            'isExistKfos'    => empty($openMain) ? false : (in_array($openMain->compareStepStatusSort(MissionTemplateEnum::STEP_STATUS_7),
                ['>', '='])), // 简单处理
            // 是否已经参与
            'appliedStatus'  => ShopOrderActivityService::getAppliedStatus($activityId, $shopId),
            // 当前用户是否是督导
            'isNoAuth'       => $shopCouncilorId == $operateUid,
            // 门店详情
            "shopDetail"     => $shopModel ? $shopModel->only(["id", "name"]) : null,
        ]);
    }

    /**
     * 霸屏通用接口
     * @param int $shopId
     * @return array
     */
    public static function getDominatingScreen(int $shopId): array
    {
        // 已废弃
        return [];


        // 查询是否有单独配置霸屏显示
        $dominatingScreenConfig = ConfigService::getConfigDetail('activity', 'dominatingScreen', getSwitchCacheTime(300))['value'] ?? [];
        if ($dominatingScreenConfig) {
            if (isset($dominatingScreenConfig['shopIds']) && (empty($dominatingScreenConfig['shopIds']) || in_array($shopId, $dominatingScreenConfig['shopIds']))) {
                return [
                    'type'       => $dominatingScreenConfig['type'] ?? 0,                      // 类型 1-消杀活动
                    'activityId' => $dominatingScreenConfig['activityId'] ?? 0,                // 活动id
                    'status'     => $dominatingScreenConfig['status'] ?? 0,                    // 门店参与活动状态 默认0-不显示霸屏 1-显示霸屏 2-显示霸屏可关闭
                    'url'        => $dominatingScreenConfig['url'] ?? '',                      // 前端跳转url
                    'title'      => $dominatingScreenConfig['title'] ?? '',                    // 内容
                    'content'    => $dominatingScreenConfig['content'] ?? '',                  // 内容
                    'frequency'  => $dominatingScreenConfig['frequency'] ?? 0,                 // 弹窗频次 1-不显示 2-仅一次 3-每天一次 4-每次打开APP
                    'confirmText'=> $dominatingScreenConfig['confirmText'] ?? 0,               // 确认按钮文案
                ];
            }

        }

        return [];
    }

}
